package action;

import dao.AddressDao;
import po.Address;
import po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;

@WebServlet(urlPatterns = "/user/action/AddressAddServlet")
public class AddressAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Address address = new Address();
        address.setAddress(req.getParameter("address"));
        address.setReceiver(req.getParameter("receiver"));
        address.setReceiverPhone(req.getParameter("receiverPhone"));
        address.setAdded(new Date());
        String userId = ((User)req.getSession().getAttribute("user")).getPhone();
        address.setUserId(userId);
        AddressDao addressDao = new AddressDao();
        addressDao.insert(address);
        Writer writer =null;
        try {
        writer = resp.getWriter();
        writer.write("yes");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
